#include "internal.h"
#include "Defines.h"
#include "DeviceProperty.h"
#include "PortableDeviceProps.h"
#include "ObjectLock.h"
#include "SensorDdi.h"
#include "Sensor.h"
#include "SensorRegistry.h"
#include "SensorRegistry.tmh"

//
// The static list of sensors supported in this driver.
//
map<wstring, CSensor *> CSensorRegistry::s_mapSensors;
CComAutoCriticalSection CSensorRegistry::s_cs;

void CSensorRegistry::RegisterSensor(const CSensor *pSensor)
{
	//
	// lock the object (though technically there *is* no object
	// here what with this being a static method and all - but
	// you get the idea - just don't want "s_vSensors" fiddled
	// with while we are doing this)
	//
	CObjectLockT<CComAutoCriticalSection> lock(&s_cs);

	//
	// add this sensor to our map
	//
	wstring id = pSensor->GetId();
	if(s_mapSensors.find(id) == s_mapSensors.end())
		s_mapSensors[id] = const_cast<CSensor*>(pSensor);
}

vector<CSensor *> CSensorRegistry::GetRegisteredSensors(bool excludeDeviceSensor)
{
	//
	// lock the object (though technically there *is* no object
	// here what with this being a static method and all - but
	// you get the idea - just don't want "s_vSensors" fiddled
	// with while we are doing this)
	//
	CObjectLockT<CComAutoCriticalSection> lock(&s_cs);

	//
	// hand out a copy of the our list of sensors
	//
	vector<CSensor *> sensors;
	for(map<wstring, CSensor *>::const_iterator it = s_mapSensors.begin() ;
		it != s_mapSensors.end() ;
		++it)
	{
		//
		// exclude the "DEVICE" sensor if we're asked to
		//
		if(excludeDeviceSensor == false || it->second->GetId() != L"DEVICE")
			sensors.push_back(it->second);
	}

	return sensors;
}

CSensor *CSensorRegistry::GetSensorById(const wstring& id)
{
	CObjectLockT<CComAutoCriticalSection> lock(&s_cs);
	if(s_mapSensors.find(id) != s_mapSensors.end())
		return s_mapSensors[id];
	return NULL;
}
